package com.bignerdranch.android.GeoQuitz;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "QuitzActivity";
    private static final String KEY_INDEX = "index";
    private static final String KEY_ANSWER = "answer";
    private static final int REQUEST_CODE_CHEAT = 0;

    private Button mTureButton;
    private Button mFalseButton;
    private ImageButton mNextButton;
    private ImageButton mPreButton;
    private Button mCheatButton;
    private TextView mQuestionTextView;
    private Question[] mQuestionBank = new Question[] {
            new Question(R.string.question_australia, true),
            new Question(R.string.question_oceans, true),
            new Question(R.string.question_mideast, false),
            new Question(R.string.question_africa, true),
            new Question(R.string.question_americas, true),
            new Question(R.string.question_asia, true)
    };

    private int mCurrentIndex;
    private ArrayList<Integer> mResult = new ArrayList<Integer>();
    private boolean mIsCheater;

    private int mCheatTimes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        logd("onCreate(Bundle) called");
        setContentView(R.layout.activity_main);

        if(savedInstanceState != null) {
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
            mResult = savedInstanceState.getIntegerArrayList(KEY_ANSWER);
        } else {
            mCurrentIndex = 0;
            for(int i = 0; i < mQuestionBank.length; i ++) {
                mResult.add(0);
            }
        }

        mQuestionTextView = findViewById(R.id.question_text_view);
        mQuestionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateCurrentIndex();
                updateQuesion();
            }
        });

        updateQuesion();

        mTureButton = findViewById(R.id.button_true);
        mTureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*String output = String.format("true");
                Toast.makeText(MainActivity.this, output, Toast.LENGTH_SHORT).show();*/

                checkAnswer(true);
            }
        });

        mFalseButton = findViewById(R.id.button_false);
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*String output = String.format("false");
                Toast toast = Toast.makeText(MainActivity.this, output, Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();*/

                checkAnswer(false);
            }
        });

        mNextButton = findViewById(R.id.button_next);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateCurrentIndex();
                mIsCheater = false;
                updateQuesion();
            }
        });

        mPreButton = findViewById(R.id.button_pre);
        mPreButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateCurrentIndex2();
                mIsCheater = false;
                updateQuesion();
            }
        });

        mCheatButton = findViewById(R.id.cheat_button);
        mCheatButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
                Intent intent = new CheatActivity().newIntent(MainActivity.this, answerIsTrue, mCheatTimes);
                //startActivity(intent);
                startActivityForResult(intent, REQUEST_CODE_CHEAT);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == REQUEST_CODE_CHEAT) {
            if(data == null) {
                return;
            }

            mIsCheater = CheatActivity.wasAnswerShown(data);
            mCheatTimes = CheatActivity.getCheatTimes(data);
        }
    }

    private void updateQuesion() {
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);
    }

    private void updateCurrentIndex() {
        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
    }

    private void updateCurrentIndex2() {
        mCurrentIndex = (mCurrentIndex + mQuestionBank.length - 1) % mQuestionBank.length;
    }

    private void checkAnswer(boolean userPressTrue) {
        if(mResult.get(mCurrentIndex) != 0) {
            Toast.makeText(this, R.string.already_answer, Toast.LENGTH_SHORT).show();
            return;
        }

        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
        int messageResId = 0;
        if(mIsCheater) {
            messageResId = R.string.judgment_toast;
        } else {
            if(userPressTrue == answerIsTrue) {
                messageResId = R.string.correct_toast;
                mResult.set(mCurrentIndex, 1);
            } else {
                messageResId = R.string.incorrect_toast;
                mResult.set(mCurrentIndex, 2);
            }
        }

        Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();

        int correctNumber = 0;
        for (int i = 0; i < mQuestionBank.length; i ++) {
            int cur = mResult.get(i);
            if(cur == 0) {
                logd("not finish anwswer !");
                return;
            } else if(cur == 1) {
                correctNumber ++;
            }
        }

        int percent = correctNumber * 100 / mQuestionBank.length;
        Toast.makeText(this, "your score is " + percent + "%", Toast.LENGTH_SHORT).show();
    }

    private void logd(String msg) {
        Log.d(TAG, msg);
    }

    @Override
    public void onStart() {
        super.onStart();
        logd("+onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        logd("+onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        logd("+onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        logd("+onStop");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        logd("+onDestroy");
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        logd("+onSaveInstanceState");
        savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
        savedInstanceState.putIntegerArrayList(KEY_ANSWER, mResult);
    }
}
